package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.manage.domain.db.BlackList;
import com.tanhua.manage.domain.vo.PageResult;
import com.tanhua.dubbo.mapper.BlackListMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class BlackListApiImpl implements BlackListApi {
    @Autowired
    private BlackListMapper blackListMapper;


    /**
     * 查询黑名单分页
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult findPageByUserId(Long userId,Long page, Long pageSize) {
        //构建分页对象
        IPage<BlackList> iPage = new Page<>(page,pageSize);
        //构建查询条件
        QueryWrapper<BlackList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.orderByDesc("created");
        //查询
        blackListMapper.selectPage(iPage,queryWrapper);
        //构建分页result
        PageResult pageResult = new PageResult();
        pageResult.setItems(iPage.getRecords());
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);
        pageResult.setCounts(iPage.getTotal());
        pageResult.setPages(iPage.getPages());

        return pageResult;


    }

    /**
     * 移除黑名单
     */
    @Override
    public void delete(Long userId,Long blackUserId) {
        QueryWrapper<BlackList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("black_user_id",blackUserId);
        blackListMapper.delete(queryWrapper);
    }
}
